package com.dao.library;

import java.util.ArrayList;

import com.bean.DBClasses.DBStudents;
import com.bean.DBClasses.DBSubjects;
import com.bean.DBClasses.DBTeacherDBSubject;
import com.bean.DBClasses.DBTeachers;
import com.bean.library.Course;
import com.bean.library.DbSubject;
import com.bean.library.Student;
import com.bean.library.Teacher;
import com.dao.BaseDao;
import com.dao.PostgreSQL;

public class TeacherDaoImpl extends BaseDao implements TeacherDao,PostgreSQL{

	@Override
	public Teacher getTeacherByTeacherIdAndPassword(String teacherId,
			String password) throws Exception {
		// TODO Auto-generated method stub
		//教师登陆(教师对应bean中的Teacher类)
		//若登陆成功，返回一个Teacher对象，包含teacherId,tacherName,teacherCourseList(教师本学期所教的所有课程)
		//若登陆失败，返回null
		/*
		if(teacherId.equals("tomato_team") && password.equals("123")){
			ArrayList<Course> teacherCourseList = new ArrayList<Course>();
			for(int i = 1; i<=3 ;i++){
				Course c = new Course("304031030", "数据库原理"+i);
				teacherCourseList.add(c);
			}
			Teacher teacher = new Teacher("000002", "Mr.Wu", password, teacherCourseList);
			return teacher;
		}
		else{
			return null;
		}
		*/
//		if(teacherId.equals("tomato_team") && password.equals("123")){
//			ArrayList<Course> teacherCourseList = new ArrayList<Course>();
//			for(int i = 1; i<=3 ;i++){
//				Course c = new Course("304031030", "数据库原理"+i);
//				teacherCourseList.add(c);
//			}
//			Teacher teacher = new Teacher("000002", "Mr.Wu", password, teacherCourseList);
//			return teacher;
//		}
//		else{
//			return null;
//		}
		
		Teacher teacher = null;
		String studentSql = "select * from teachers where teacher_id = ? and pass_word = ?";
		DBTeachers dataBaseTeachers = esql.query(DBTeachers.class, studentSql,
				teacherId, password);
		System.out.println("teacherId:--"+teacherId);
		System.out.println("password:---"+password);
		if(dataBaseTeachers==null){
			return null;
		}
		if(dataBaseTeachers.getTeacherName()==null){
			return null;
		}
		if (!dataBaseTeachers.getTeacherName().isEmpty()) {
			System.out.println("密码正确！");
			ArrayList<Course> teacherCourseList = new ArrayList<Course>();
			String teacherSubjectSql = "select * from subjects,teacher_subject where teacher_subject.teacher_id = ? and teacher_subject.subject_id = subjects.subject_id";
			ArrayList<DBTeacherDBSubject> dbSubjectList = (ArrayList<DBTeacherDBSubject>)
					esql.list(DBTeacherDBSubject.class, teacherSubjectSql,dataBaseTeachers.getTeacherId());
			int courseCount = dbSubjectList.size();
			for(int i = 0;i<courseCount;i++){
				Course course = new Course();
				course.setCourseId(dbSubjectList.get(i).getSubjectId());
				course.setCourseName(dbSubjectList.get(i).getSubjectName());
				teacherCourseList.add(course);
			}
			teacher = new Teacher(dataBaseTeachers.getTeacherId()
					,dataBaseTeachers.getTeacherName()
					,dataBaseTeachers.getPassWord()
					,teacherCourseList);
		}

		return teacher;
	}	
}
